// 网关配置的前缀
var jobsServer = "/jobsSer"

var currentPage = 1;    // 当前页
var pageSize = 10;      // 页大小
var total = 0;          // 数据总量
var pages = 1;          // 页数总量

let isBookmark = false;
let bookmarkList = [];

// let userId = "1354255569179971599";
// let userName = "JEWELLTHO";
let userId = "";
let userName = "";


let companyName = "";
let hrId = "";
let hrName = "";
let jobId = "";
let companyId = "";
let jobName = "";
let huntingStatus = "已送达";

let Authorization = "";
let refreshToken = "";

// 加载页面时读取后台的 ip
window.onload = function () {
    $.ajaxSettings.async = false;
    $.getJSON("/js/properties.json", function (data) {
        jobsServer = data.jobsServer;
    });

    // 获取令牌
    Authorization = getCookie("Authorization")
    refreshToken = getCookie("refreshToken")
    userId = $("#userId").html();
    userName = $("#userName").html();
    jobId = $("#jobId").val();
    companyId = $("#companyId").val();

    getBookmarkList();
    getJobDetail();
    getJobList(1, 5, companyId);
    isPostProfile();
}

function getJobDetail() {

    $.ajax({
        type: "get",
        async: false,
        url: jobsServer + "/jobs/jobInfo/detail?jobId=" + jobId + "&companyId=" + companyId,
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        success: function (data) {
            console.log("岗位详情");

            let job = data.datas;
            let company = job.company;

            $("#jobName").html(job.jobName);
            $("#updateDate").html(job.updateDate);
            $("#salaryMin").html(job.salaryMin);
            $("#salaryMax").html(job.salaryMax);
            $("#salaryUnit").html(job.salaryUnit);
            $("#jobType").html(job.jobType);
            $("#industry").html(job.industry);
            $("#experience").html(job.experience);
            $("#introduce").html(job.introduce);
            $("#requirement").html(job.requirement);

            $("#companyAddress").html(company.companyAddress);
            $("#introduction").html(company.introduction);

            $("#logo").attr("src", company.logo);

            let skillList = [];
            if (job.skills !== null && job.skills !== "") {
                skillList = job.skills.split(",");
            }
            let $skills = $("#skills");
            $skills.empty();
            $.each(skillList, function (i, skill) {
                $skills.append(
                    "<a>" + skill + "</a>"
                )
            });

            companyName = company.companyName;
            hrId = job.hrId;
            hrName = job.hrName;
            jobId = job.jobId;
            jobName = job.jobName;


        },
        error: function (err) {
            cocoMessage.error(2000, err.statusText, function () {
                console.log(err);
            });
        }

    });

}

function getBookmarkList() {
    $.ajax({
        type: "GET",
        async: false,
        url: jobsServer + "/jobs/getBookmarks?userId=" + userId,
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        success: function (data) {
            bookmarkList = data.datas;
            if ($.inArray(jobId, bookmarkList) >= 0) {
                $("#followBut i").attr("hidden", "hidden");
                $("#followBut img").removeAttr("hidden");
                isBookmark = true;
            }
        },
        error: function (err) {
            cocoMessage.error(2000, err.statusText, function () {
                console.log(err);
            });
        }
    })
}

function updateBookmark() {
    let url = "";
    if (isBookmark) {
        url = jobsServer + "/jobs/cancelBookmarkJob";
    } else {
        url = jobsServer + "/jobs/bookmarkJob";
    }
    $.ajax({
        type: "POST",
        async: false,
        url: url,
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        data: {
            "userId": userId,
            "jobId": jobId,
        },
        success: function (data) {
            if (data.resp_code === 0) {
                // 成功
                cocoMessage.info(2000, "收藏成功", function () {
                });
                if (isBookmark) {
                    console.log("取消收藏")
                    $("#followBut img").attr("hidden", "hidden");
                    $("#followBut i").removeAttr("hidden");
                    isBookmark = false;
                } else {
                    console.log("收藏")
                    $("#followBut i").attr("hidden", "hidden");
                    $("#followBut img").removeAttr("hidden");
                    isBookmark = true;
                }
            } else {
                // 失败
                cocoMessage.error(2000, "收藏失败", function () {
                    console.log(data.resp_msg);
                });
            }
        },
        error: function (err) {
            // 异常
            cocoMessage.error(2000, err.statusText, function () {
                console.log(err);
            });
        }
    })
}

function getJobList(currentPage, pageSize, companyId) {

    $.ajax({
        type: "get",
        async: false,
        url: jobsServer + "/jobs/jobInfo",
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        data: {
            currentPage: currentPage,
            pageSize: pageSize,
            companyId: companyId,
            status: "正常"
        },
        success: function (data) {
            console.log("当前企业的其他岗位");

            //清空数据
            $("#jobsList").empty();
            //返回的数据用data.rows获取内容
            $.each(data.datas.records, function (i, item) {

                $("#jobsList").append(
                    "<div class='col-lg-12'>" +
                    "<div class='single-job style-two'>" +
                    "<div class='info-top'>" +
                    "<div class='job-info'>" +
                    "<div class='job-info-inner'>" +
                    "<div class='job-info-top'>" +
                    "<div class='title-name'>" +
                    "<h5 class='job-title'>" +
                    "<a onclick='jobDetail(\"" + item.jobId + "\", \"" + item.companyId + "\")'>" + item.jobName + "</a>" +
                    "</h5>" +
                    "</div>" +
                    "</div>" +
                    "<div class='job-meta-two flex-wrap'>" +
                    "<div class='field-salary_from'>" +
                    "<i class='gj-icon gj-icon-money'></i>" +
                    "￥&nbsp;" + item.salaryMin + "&nbsp;-&nbsp;" + item.salaryMax + "&nbsp;" + item.salaryUnit +
                    "</div>" +
                    "<div class='field-datetime'>" +
                    "<i class='lnr lnr-clock'></i>" + item.updateDate +
                    "</div>" +
                    "</div>" +
                    "</div>" +
                    "</div>" +
                    "</div>" +
                    "</div>" +
                    "</div>"
                );

            });
        },
        error: function (err) {
            cocoMessage.error(2000, err.statusText, function () {
                console.log(err);
            });
        }

    });

}

function postProfile() {
    $.ajax({
        type: "PUT",
        async: false,
        url: jobsServer + "/jobs/huntingInfo",
        headers: {
            "Content-Type": "application/json",
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        data: JSON.stringify({
            "userId": userId,
            "userName": userName,
            "companyId": companyId,
            "companyName": companyName,
            "hrId": hrId,
            "hrName": hrName,
            "jobId": jobId,
            "jobName": jobName,
            "huntingStatus": huntingStatus
        }),
        success: function (data) {
            if (data.resp_code === 0) {
                // 成功
                cocoMessage.info(2000, "简历投递成功", function () {
                });
                $(".post").attr("hidden", "hidden");
                $(".yet-post").removeAttr("hidden");
            } else {
                // 失败
                cocoMessage.error(2000, "简历投递失败", function () {
                    console.log(data.resp_msg);
                });
            }
        },
        error: function (err) {
            // 异常
            cocoMessage.error(2000, err.statusText, function () {
                console.log(err);
            });
        }
    })
}

function isPostProfile() {
    $.ajax({
        type: "GET",
        async: false,
        url: jobsServer + "/jobs/selectHuntingByIds",
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        data: {
            "userId": userId,
            "companyId": companyId,
            "hrId": hrId,
            "jobId": jobId
        },
        success: function (data) {
            unauthorized(data);

            let hunting = data.datas
            if (hunting !== null && hunting !== "" && hunting.length > 0) {
                $(".post").attr("hidden", "hidden");
                $(".yet-post").removeAttr("hidden");
            }
        },
        error: function (err) {
            // 异常
            cocoMessage.error(2000, err.statusText);
        }
    })
}

function jobDetail(jobId, companyId) {
    location.href = "/oa/jobs/jobDetails/" + jobId + "/" + companyId;
}
